/**
 * Created by mkDark on 2018/3/20.
 */
const moment = require('moment');
const async = require('async');
const superagent = require('superagent');
const activityService = require('../../../service/activityService');


const activityRouterUtil = (function () {

    function activityRouterUtil() {

    }

    /**
     * 获取活动详情
     *
     * @author 王俊
     * @date 2018-07-31
     *
     * @param {{Object}} options
     * @param {Number} options.device
     * @param {Number} options.actId
     * @param {Number} options.userId
     * @param {Number} options.participantLimit
     * @param {Function} callback
     */
    activityRouterUtil.getImagePraiseActivityDetail = function (options, callback) {
        options.type = 20; // 标记图片活动

        const totalTasks = [];

        // 获取活动详情
        totalTasks.push(function (callback) {
            const opt = {
                device: options.device,
                type: 20,
                activityId: options.actId,
                remaining: true,
                participants: true,
                rule: true
            };
            activityService.getActivityDetail(opt, function (error, actInfo) {
                const data = {};
                data.actInfo = actInfo;
                return callback(error, data);
            });
        });

        // 获取活动参与者列表
        totalTasks.push(function (data, callback) {
            const opt = {
                actIds: [options.actId],
                rewardLimit: 1,
                pageNum: 0,
                pageSize: options.participantLimit,
                withTotalPage: 0,
                orderBy: 1
            };
            activityService.getImagePraiseActivityParticipantList(opt, function (error, participant) {
                data.participant = participant || {};
                return callback(error, data);
            });
        });

        // 如果有userId, 尝试获取作品
        totalTasks.push(function (data, callback) {
            if(!options.userId) {
                return callback(null, data);
            }
            const opt = {
                actId: options.actId,
                userId: options.userId
            };
            activityService.getImagePraiseActivityProductInfo(opt, function (error, info) {
                if(info) {
                    data.productInfo = info;
                    data.productInfo.id = info.aup_id;
                }
                return callback(error, data);
            });
        });
        // 如果有作品, 尝试获取给该作品点赞的用户列表
        totalTasks.push(function (data, callback) {
            if(!options.userId) {
                return callback(null, data);
            }
            if(!data.productInfo) {
                return callback(null, data);
            }
            const opt = {
                productId: data.productInfo.id,
                rewardType: 32,     // 被点赞红包
            };
            activityService.getImagePraiseActivityProductLikeUserList(opt, function (error, likeUsers) {
                if(likeUsers)data.productInfo.likePeopleList = likeUsers.list || [];
                return callback(error, data);
            })
        });

        // 重组数据
        totalTasks.push(function (data, callback) {
            data.retData = {
                activity: data.actInfo,
                rewardInfo: {
                    randomParticipants: data.actInfo.randomParticipants,
                    randomRemaining: data.actInfo.randomRemaining,
                    randomMin: data.actInfo.randomMin,
                    randomMax: data.actInfo.randomMax,
                    likeParticipants: data.actInfo.likeParticipants,
                    likeRemaining: data.actInfo.likeRemaining,
                    likeMin: data.actInfo.likeMin,
                    likeMax: data.actInfo.likeMax,
                    praisedParticipants: data.actInfo.praisedParticipants,
                    praisedRemaining: data.actInfo.praisedRemaining,
                    praisedMin: data.actInfo.praisedMin,
                    praisedMax: data.actInfo.praisedMax
                },
                participantList: [],
            };
            for(let i=0; i<data.participant.list.length; i++) {
                const p = data.participant.list[i];
                data.retData.participantList.push({
                    profilePhoto: p.profilePhoto,
                    rewardAmt: p.reward
                })
            }
            if(data.productInfo) {
                data.retData.productInfo = {
                    id: data.productInfo.id,
                    url: data.productInfo.url,
                    authorName: data.productInfo.authorName,
                    authorIcon: data.productInfo.authorIcon,
                    likeReward: data.productInfo.likeReward,
                    likePeopleCnt: data.productInfo.likePeopleCnt,
                    state: data.productInfo.state,
                    note: data.productInfo.note,
                    likePeopleList: data.productInfo.likePeopleList
                };
            }
            // 删除多余字段

            return callback(null, data);
        });

        async.waterfall(totalTasks, function (error, data) {
            if(error) return callback(error, data);
            return callback(error, data.retData);
        })
    };

    /**
     * 获取作品详情
     *
     * @author 王俊
     * @date 2018-07-31
     *
     * @param {{Object}} options
     * @param {Number} options.device
     * @param {Number} options.productId
     * @param {Number} [options.likeLimit]
     * @param {Number} [options.likeRewardType]
     * @param {Number} [options.userId]
     * @param {Function} callback
     */
    activityRouterUtil.getProductDetail = function(options, callback) {
        const totalTasks = [];
        options.likeLimit = options.likeLimit || 20;

        // 获取作品详情
        totalTasks.push(function (callback) {
            const opt = {
                productId: options.productId
            };
            activityService.getImagePraiseActivityProductInfo(opt, function (error, productInfo) {
                const data = {};
                data.productInfo = productInfo;
                if(!productInfo) error = error || '该作品不存在或已删除';
                return callback(error, data);
            })
        });

        // 获取作品对应的点赞用户列表
        totalTasks.push(function (data, callback) {
            const opt = {
                productId: options.productId,
                pageNum: 0,
                pageSize: options.likeLimit,
                rewardType: options.likeRewardType,        // 点赞奖励
            };
            activityService.getImagePraiseActivityProductLikeUserList(opt, function (error, userList) {
                data.productInfo.likePeopleList = userList.list || [];
                return callback(error, data);
            })
        });

        // 获取活动详情
        totalTasks.push(function (data, callback) {
            const opt = {
                device: options.device,
                type: 20,
                activityId: data.productInfo.act_id,
                remaining: 1,
                participants: 1,
                rule: 1
            };
            activityService.getActivityDetail(opt, function (error, actInfo) {
                data.actInfo = actInfo;
                return callback(error, data);
            });
        });

        // 按需确认该用户是否给当前作品点过赞
        totalTasks.push(function (data, callback) {
            if(!options.userId) {
                return callback(null, data);
            }
            const opt = {
                userId: options.userId,
                productId: options.productId
            };
            activityService.checkUserWhetherLikeProduct(opt, function (error, liked) {
                data.userLiked = liked;
                return callback(error, data);
            });
        });

        // 重组数据
        totalTasks.push(function (data, callback) {
            data.retData = {
                activity: data.actInfo,
                rewardInfo: {
                    randomParticipants: data.actInfo.randomParticipants,
                    randomRemaining: data.actInfo.randomRemaining,
                    randomMin: data.actInfo.randomMin,
                    randomMax: data.actInfo.randomMax,
                    likeParticipants: data.actInfo.likeParticipants,
                    likeRemaining: data.actInfo.likeRemaining,
                    likeMin: data.actInfo.likeMin,
                    likeMax: data.actInfo.likeMax,
                    praisedParticipants: data.actInfo.praisedParticipants,
                    praisedRemaining: data.actInfo.praisedRemaining,
                    praisedMin: data.actInfo.praisedMin,
                    praisedMax: data.actInfo.praisedMax
                },
                participantList: [],
            };
            if(data.productInfo) {
                data.retData.productInfo = {
                    id: data.productInfo.id,
                    url: data.productInfo.url,
                    authorName: data.productInfo.authorName,
                    authorIcon: data.productInfo.authorIcon,
                    likeReward: data.productInfo.likeReward,
                    likePeopleCnt: data.productInfo.likePeopleCnt,
                    state: data.productInfo.state,
                    note: data.productInfo.note,
                    likePeopleList: data.productInfo.likePeopleList
                };
            }
            if(options.userId) {
                data.retData.likeControl = {
                    liked: data.userLiked,
                    canLike: (!data.userLiked && options.userId !== data.productInfo.user_id) ? 1 : 0,
                };
                if(!data.retData.likeControl.canLike) {
                    if(data.retData.likeControl.liked) {
                        data.retData.likeControl.reason = '您已为该作品点过赞';
                    }
                    if(options.userId === data.productInfo.user_id) {
                        data.retData.likeControl.reason = '不可以为自己的作品点赞';
                    }
                }
            }
            return callback(null, data);
        });

        async.waterfall(totalTasks, function (error, data) {
            if(error) return callback(error, null);
            return callback(error, data.retData);
        });



    };

    return activityRouterUtil;
})();

module.exports = activityRouterUtil;